package com.pine.manager.pineframework.trigger.impl;

import com.pine.common.beans.exception.BusinessException;
import com.pine.common.beans.exception.ExceptionCodeEnum;
import com.pine.common.beans.pineframework.request.SysRoleSaveOrUpdateRequest;
import com.pine.common.trigger.AbstractTriggerTemplate;
import com.pine.common.trigger.TriggerContext;
import com.pine.common.util.user.UserInfo;
import com.pine.common.util.user.UserUtil;
import com.pine.dao.pineframework.entity.SysRole;
import com.pine.manager.core.TriggerTypeEnum;
import com.pine.manager.pineframework.SysRoleManager;
import com.pine.manager.pineframework.bo.SysRoleBo;
import com.pine.manager.pineframework.convertors.SysRoleConvertor;
import com.pine.manager.pineframework.query.SysRoleQuery;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 系统角色表新增或修改数据 触发器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-13
 */
@Component
@Slf4j
public class SysRoleSaveOrUpdateTrigger extends AbstractTriggerTemplate<SysRoleSaveOrUpdateRequest, Boolean> {

    @Resource
    private SysRoleManager sysRoleManager;

    @Resource
    private SysRoleConvertor sysRoleConvertor;

    private boolean isSave;

    private SysRoleBo oldSysRoleBo;

    public SysRoleSaveOrUpdateTrigger() {
        super(TriggerTypeEnum.SYS_ROLE_SAVE_OR_UPDATE);
    }

    @Override
    protected void before(TriggerContext<SysRoleSaveOrUpdateRequest> triggerContext) {
        SysRoleSaveOrUpdateRequest request = triggerContext.getTriggerRequest();
        isSave = Objects.isNull(request.getId());

        // 尝试获取旧角色信息
        SysRoleQuery query = new SysRoleQuery();
        query.setId(request.getId());
        if (isSave) {
            query.setCode(request.getCode());
        }
        oldSysRoleBo = sysRoleManager.getOne(query);

        // 校验
        if (isSave) {
            checkOnSave();
        } else {
            checkOnUpdate();
        }
    }

    /**
     * 触发
     *
     * @param triggerContext 触发上下文
     * @return 触发结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean trigger(TriggerContext<SysRoleSaveOrUpdateRequest> triggerContext) {
        SysRoleSaveOrUpdateRequest request = triggerContext.getTriggerRequest();

        // 转换为业务对象，并设置额外字段
        SysRoleBo newSysRoleBo = sysRoleConvertor.saveRequestToEntityBo(request);
        setUserInfo(newSysRoleBo);

        // 转换为实体
        SysRole sysRole = sysRoleConvertor.entityBoToEntity(newSysRoleBo);

        // 新增
        if (isSave) {
            return sysRoleManager.save(sysRole);
        }
        // 修改
        return sysRoleManager.updateById(sysRole);
    }


    private void checkOnSave() {
        // 校验角色编码是否重复
        if (Objects.nonNull(oldSysRoleBo)) {
            throw new BusinessException(ExceptionCodeEnum.USER_ROLE_CODE_EXIST);
        }
    }

    private void checkOnUpdate() {
        // 校验角色是否存在
        if (Objects.isNull(oldSysRoleBo)) {
            throw new BusinessException(ExceptionCodeEnum.USER_ROLE_NOT_EXIST);
        }
    }

    private void setUserInfo(SysRoleBo sysRoleBo) {
        UserInfo userInfo = UserUtil.get();
        if (Objects.nonNull(userInfo)) {
            if (isSave) {
                sysRoleBo.setCreateUserId(userInfo.id());
            }
            sysRoleBo.setUpdateUserId(userInfo.id());
        }
    }
}
